import React from 'react'
import { Route, Switch, Redirect } from 'react-router-dom';

import Suggestion from './Suggestion';
import Hot from './Hot';
import User from './User';
import All from './All';
import Alter from './Alter';
import Aucai from './Aucai';
import Login from './Login';
import Header from './Header';
import { useAuth } from './AuthProvider';
import './css/content.css'


const PrivateRoute = ({ component: Com,children, ...rest }) => {
    return <Route {...rest} render={(props) => {
        if (!sessionStorage.getItem('isLogin')) {
            alert('请先登录');
            return <Redirect to={{
                pathname: '/',
                state: props.location
            }} />
        }
        return Com?<Com/>:children;
    }
    } />
}


const Content = () => {
    const {isLogin} = useAuth();
    return (
        <div className='content'>
        {
            !isLogin?<Route path='/'><Login /></Route>    
            :<div className='realContent'>
                <Header />
                <Switch>
                    <PrivateRoute path='/alter' component={Alter}></PrivateRoute>
                    <PrivateRoute path='/hot' component={Hot}></PrivateRoute>
                    <PrivateRoute path='/suggestion' component={Suggestion}></PrivateRoute>
                    <PrivateRoute path='/adminUser' component={User}></PrivateRoute>
                    <PrivateRoute path='/user' component={All}></PrivateRoute>
                    <PrivateRoute path='/allSu' component={Aucai}></PrivateRoute>
                </Switch>
            </div>
        }
        </div>
        
    )
}

export default Content